﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Game_Engine
{
    public class Agent : GameObject
    {
        private List<Vector3> path;
        private Vector3 location;
        private Vector3 destination;
        private AStarSearch searchArea;
        private int pathIndex;
        private float pathProgress;

        public List<Vector3> Path
        {
            get{return path;}
        }

        public Vector3 Location
        {
            get{return location;}
            set { location = value; }
        }

        public Vector3 Destination
        {
            get{return destination;}
            set { destination = value; }
        }

        public AStarSearch SearchArea
        {
            get { return searchArea; }
            set { searchArea = value; }
        }

        public Agent()
        {
            components = new List<Component>();
            path = new List<Vector3>();
        }

        public void newSearch()
        {
            Random random = new Random();
            path.Clear();

            foreach (AStarNode node in searchArea.Nodes)
                node.Passable = true;

            for (int i = 0; i < searchArea.ImpassableNodes; i++)
            {
                int x = random.Next(0, searchArea.Cols);
                int y = random.Next(0, searchArea.Rows);

                searchArea.Nodes[x, y].Passable = false;
            }

            searchArea.Start = searchArea.Nodes[random.Next(0, searchArea.Cols),
                                                  random.Next(0, searchArea.Rows)];
            searchArea.Start.Passable = true;
            searchArea.End = searchArea.Nodes[random.Next(0, searchArea.Cols),
                                                  random.Next(0, searchArea.Rows)];
            searchArea.End.Passable = true;

            

            Console.WriteLine("\nStart: " + searchArea.Start.Position);
            Console.WriteLine("End: " + searchArea.End.Position);

            searchArea.Search();

            Console.WriteLine("SizeX: " + searchArea.Cols);
            Console.WriteLine("SizeY: " + searchArea.Rows);

            AStarNode current = searchArea.End;
            while (current != null)
            {
                path.Insert(0, current.Position);
                Console.Write("\n" + current.Position);
                current = current.Parent;
            }

            pathIndex = path.Count - 1;
            pathProgress = 0;
        }

        public void moveAgent(Camera camera)
        {
            Renderer r = (Renderer)GetComponent(typeof(Renderer));

            /*foreach (Vector3 position in path)                  //Render over time using LERP
            {                                                   //between elements of the path
                Transform.LocalPosition = position;             //list to get a nice animation
                r.Draw(camera, ScreenManager.GraphicsDevice);
            }*/

            if (pathIndex > 0)
            {
                Transform.LocalPosition = Vector3.Lerp(path[pathIndex], path[pathIndex - 1], pathProgress);
                pathProgress += 0.1f;
                if (pathProgress > 1)
                {
                    pathIndex--;
                    pathProgress = 0;
                }
            }
 
            r.Draw(camera, ScreenManager.GraphicsDevice);
        }

        public void leaveTrail(Camera camera)
        {
            Renderer r = (Renderer)GetComponent(typeof(Renderer));

            for (int i = path.Count - 1; i > pathIndex; i--)
            {
                Transform.LocalPosition = path[i];
                r.Draw(camera, ScreenManager.GraphicsDevice);
            }
        }
    }
}
